/*
 * Copyright (C) Huawei Technologies Co., Ltd. 2016. All rights reserved.
 * See LICENSE.txt for this sample's licensing information.
 */

package com.mifengs.mall.receiver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.huawei.hms.support.api.push.PushReceiver;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.entity.PullMsgBean;
import com.mifengs.mall.ui.main.MainActivity;
import com.mifengs.mall.util.ToIntentUtil;


/**
 * 应用需要创建一个子类继承com.huawei.hms.support.api.push.PushReceiver，
 * 实现onToken，onPushState ，onPushMsg，onEvent，这几个抽象方法，用来接收token返回，push连接状态，透传消息和通知栏点击事件处理。
 * onToken 调用getToken方法后，获取服务端返回的token结果，返回token以及belongId
 * onPushState 调用getPushState方法后，获取push连接状态的查询结果
 * onPushMsg 推送消息下来时会自动回调onPushMsg方法实现应用透传消息处理。本接口必须被实现。 在开发者网站上发送push消息分为通知和透传消息
 *           通知为直接在通知栏收到通知，通过点击可以打开网页，应用 或者富媒体，不会收到onPushMsg消息
 *           透传消息不会展示在通知栏，应用会收到onPushMsg
 * onEvent 该方法会在设置标签、点击打开通知栏消息、点击通知栏上的按钮之后被调用。由业务决定是否调用该函数。
 */
public class HuaweiPushRevicer extends PushReceiver {
    private XGMsgThead msgThead;
    private  PullMsgBean pullMsgBean;
    private Context mContext;
	public static final String TAG = "HuaweiPushRevicer";

	public static final String ACTION_UPDATEUI = "action.updateUI";

    public static String token;



    @Override
    public void onToken(Context context, String tokenIn, Bundle extras) {
    	String belongId = extras.getString("belongId");
        token = tokenIn;

        Intent intent = new Intent();
        intent.setAction(ACTION_UPDATEUI);
        intent.putExtra("log", "belongId为:" + belongId + " Token为:" + token);
        context.sendBroadcast(intent);
    }

    @Override
    public boolean onPushMsg(Context context, byte[] msg, Bundle bundle) {
        try {
        	//CP可以自己解析消息内容，然后做相应的处理
            String content = new String(msg, "UTF-8");
            Intent intent = new Intent();
            intent.setAction(ACTION_UPDATEUI);
            intent.putExtra("log", "收到PUSH透传消息,消息内容为:" + content);
            context.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public void onEvent(Context context, Event event, Bundle extras) {
//        if (Event.NOTIFICATION_OPENED.equals(event) || Event.NOTIFICATION_CLICK_BTN.equals(event)) {
//            int notifyId = extras.getInt(BOUND_KEY.pushNotifyId, 0);
//            Intent intent = new Intent();
//            intent.setAction(ACTION_UPDATEUI);
//            intent.putExtra("log", "收到通知栏消息点击事件,notifyId:" + notifyId);
//            context.sendBroadcast(intent);
//            if (0 != notifyId) {
//                NotificationManager manager = (NotificationManager) context
//                        .getSystemService(Context.NOTIFICATION_SERVICE);
//                manager.cancel(notifyId);
//            }
//        }
        mContext = context;
        String ster= extras.getString(BOUND_KEY.pushMsgKey);

        try {
//            JSONArray jsonArray=new JSONArray(ster);
//            JSONObject obj =jsonArray.getJSONObject(0);
//            String str = obj.getString("android_message");
            String strTemp =  ster.replace("{","").replace("}","");
            String temp = strTemp.replace("[","{").replace("]","}");
            pullMsgBean =new Gson().fromJson(temp,PullMsgBean.class);
            msgThead = new XGMsgThead();
            msgThead.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onEvent(context, event, extras);
    }

    @Override
    public void onPushState(Context context, boolean pushState) {

        Intent intent = new Intent();
        intent.setAction(ACTION_UPDATEUI);
        intent.putExtra("log", "Push连接状态为:" + pushState);
        context.sendBroadcast(intent);
    }

    private void toIntent(){
        try {
            if(pullMsgBean.getType().equals("1")){
                if(!"".equals(pullMsgBean.getAppPage())){
                    if(!pullMsgBean.getAppPage().equals("0")){
                        Intent  intent = new Intent(mContext,MainActivity.class);
                        intent.putExtra("isPush",true);
                        int page = Integer.parseInt(pullMsgBean.getAppPage());
                        intent.putExtra("msgPage",page);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                }
            }else  if(pullMsgBean.getType().equals("2")){
                if(!"".equals(pullMsgBean.getRedirectUrl())){
                    ToIntentUtil.toCommonIntent(pullMsgBean.getRedirectUrl(),mContext);
                }
            }else  if(pullMsgBean.getType().equals("3")){
                if(PreferenceUtils.getLoginStatus()){
                    ToIntentUtil.toCommonIntent(pullMsgBean.getRedirectUrl(),mContext);
                }
            }
        }catch (Exception e){

        }

    }

    Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    toIntent();
                    if (msgThead != null) {
                        if (msgThead.isAlive()) {
                            msgThead.interrupt();
                        }
                    }
                    break;
            }
        }
    };

    public class XGMsgThead extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Message msg = new Message();
            msg.what = 1;
            mHandle.sendMessage(msg);
        }
    }
}
